<?php
namespace Shopmanage\Model;
use Think\Model;
class EventsModel extends Model {
	public $eid;
	public $start_time;
	public $end_time;
	public $except_eid;
	public $sid;
	public $c;
	public function check_time(){
		$where = "`sid`=".$this->sid." AND ((`start_time`<='".$this->start_time."' AND `end_time`>='".$this->start_time."')";
		$where .= " OR (`start_time`<='".$this->end_time."' AND `end_time`>='".$this->end_time."'))";
		$where .= $this->except_eid?" AND `id`<>".$this->except_eid:""; //修改活动时排除当前活动
		$time_duble = $this->where($where)->getField("`id`");
		return $time_duble;
	}
	public function prepare_data(){
		// $data["curtool"] = I("post.curtool")?I("post.curtool"):session("curtool");
		$data["start_time"] = I("post.start_time");
		$data["end_time"] = I("post.end_time");
		$data["sid"] = $this->sid;
		$data["game_lv1_times"] = I("post.game_lv1_times");
		$data["game_lv2_times"] = I("post.game_lv2_times");
		$data["game_lv3_times"] = I("post.game_lv3_times");
		$data["game_def_times"] = I("post.game_def_times");
		$data["range_time"] = I("post.range_time");
		$data["withdraw"] = I("post.with_draw_time");
		$data["tms_newcomer"] = I("post.tms_newcomer");
		$data["likeme"] = I("post.like")?1:0;
		$data["collect"] = I("post.collect")?1:0;
		$data["followme"] = I("post.follow")?1:0;
		$data["sumpay"] = I("post.lj_money");
		$data["advancedset"] = I("post.advancedset");
		$data["taobao_user_id"] = session("VISITOR_ID");
		$data["usernick"] = session("VISITOR_NICK");
		$data["logintime"] = session("logintime")?session("logintime"):date("Y-m-d H:i:s");
		I("post.ostart_time")?$data["order_starttime"] = I("post.ostart_time"):"";
		I("post.oend_time")?$data["order_endtime"] = I("post.oend_time"):"";
		I("post.ordertimeset")?$data["order_timeset"] = I("post.ordertimeset"):"";
		I("post.brandme")?$data["brandid"] = I("post.brandid"):$data["brandid"]=0;
		I("post.tms_everyday")?$data["tms_everyday"] = I("post.tms_everyday"):$data["tms_everyday"]=0;
		I("post.andor")=="or"?$data["andor"] = 1:$data["andor"]=0;
		$data["orderset"] = I("post.orderset");
		$data["cart_items"] = I("post.cart_items");
		$data["tms_cartadd"] = I("post.tms_cartadd");
		$data["tms_cartadd_limit"] = I("post.tms_cartadd_limit");
		if($data["cart_items"]){
			Vendor('taobaosdk.top.request.ItemsSellerListGetRequest');
			$req = new \ItemsSellerListGetRequest;
			$req->setFields("pic_url");
			$req->setNumIids($data["cart_items"]);
			$resp = $this->c->execute($req, session("access_token"));
			foreach($resp->items->item as $item){
				$pic_urls[]=$item->pic_url;
			}
			$data["cart_items_pics"]=(implode(",",$pic_urls));
		}else{
			$data["cart_items_pics"]='';
		}
		if(!I("post.cartset")){
			$data["tms_cartadd"]=0; //关闭购物车抽奖
		}
		if(I("post.orderset")==1){
			if(I("post.order_payment_overamont")=="--"){
			$data["order_payment_addon"]=10000;
			}else{
			$data["order_payment_addon"] = I("post.order_payment_overamont");
			}
			$data["order_items"]=I("post.order_items");
			$data["tms_goodrate"]=I("post.tms_goodrate");
			I("post.order_pay_time_start")?$data["order_pay_time_start"]=I("post.order_pay_time_start"):"";
			I("post.order_pay_time_end")?$data["order_pay_time_end"]=I("post.order_pay_time_end"):"";
			$data["order_pay_times"]=I("post.order_pay_times");
			$data["order_success_times"]=I("post.order_success_times");
			$data["order_payment"] = I("post.order_payment");
			$data["goodrate_times"] = I("post.goodrate_times")?I("post.goodrate_times"):0;
			$data["goodrate_kwd"] = I("post.goodrate_kwd");
		}
		if(I("post.custom")=="newcomer"){
			$data["trads"] = 0;
		}
		if(I("post.custom")=="oldcomer"){
			$data["trads"] = 1;
		}
		if(I("post.custom")=="chkjy"){
			$data["trads"] = I("post.jy_tms");
		}
		if(I("post.rate")=="no_bad"){
			$data["goodrates"] = 0;
		}
		if(I("post.rate")=="all_good"){
			$data["goodrates"] = 1;
		}
		if(I("post.no_refond")){
			$data["norefound"] = 1;
		}
		$data["everytrades"] = I("post.everytrades")?I("post.everytrades"):0;
		$data["everytrades_times"] = I("post.everytrades_times")?I("post.everytrades_times"):0;
		$data["everysumpay"] = I("post.everysumpay")?I("post.everysumpay"):0;
		$data["everysumpay_times"] = I("post.everysumpay_times")?I("post.everysumpay_times"):0;
		$data["jointime"] = date("Y-m-d H:i:s");
		return $data;
	}
	public function getCfg(){
		$cfg = current($this->where("`sid`=".$this->sid." AND `id`=".$this->eid)->getField("`id`,`curtool`, `start_time`, `end_time`, `range_time`, `withdraw`, `brandid`, `orderset`, `none_anim`, `andor`, `game_lv1_times`, `game_lv2_times`, `game_lv3_times`, `game_def_times`, `goodrate_times`, `everytrades`, `everytrades_times`, `everysumpay`, `everysumpay_times`, `order_payment`, `order_payment_addon`, `order_pay_time_start`, `order_pay_time_end`, `order_success_time_start`, `order_success_time_end`, `tms_goodrate`, `goodrate_kwd`, `cart_items`, `tms_cartadd`, `tms_cartadd_limit`, `cart_items_pics`, `order_items`, `order_success_times`, `order_pay_times`, `tms_newcomer`, `tms_pay`, `tms_finish`, `tms_sumpay`, `tms_rated`, `tms_collect`, `tms_like`, `tms_follow`, `tms_everyday`, `likeme`, `shareme`, `followme`, `collect`, `advancedset`, `sumpay`, `trads`, `goodrates`, `norefound`, `buyer_signday`, `lotts`, `wins`, `usernick`, `mixednick`, `jointime`"));
		if(C("TAE")){
			$cfg["order_items_count"] = $cfg["order_items"]?count(json_decode(urldecode_utf8($cfg["order_items"]))):0;
			$cfg["cart_items_count"] = $cfg["cart_items"]?count(json_decode(urldecode_utf8($cfg["cart_items"]))):0;
		}else{
			$cfg["order_items_count"] = $cfg["order_items"]?count(json_decode(urldecode($cfg["order_items"]))):0;
			$cfg["cart_items_count"] = $cfg["cart_items"]?count(json_decode(urldecode($cfg["cart_items"]))):0;
		}
		// $cfg["order_items_count"] = $cfg["order_items"]?count(explode(",",$cfg["order_items"])):0;
		// $cfg["cart_items_count"] = $cfg["cart_items"]?count(explode(",",$cfg["cart_items"])):0;
		$cfg["cartset"] = $cfg["tms_cartadd"]==0?0:1;
		$cfg["order_payment_addon"] = $cfg["order_payment_addon"]==10000?"--":$cfg["order_payment_addon"];
		return $cfg;
	}
	public function getFrontCfg(){
		
		if($this->eid){
			// 如果有读取则为修改配置
			$cfg["frontcfg"] = current($this->where("`sid`=".$this->sid." AND `id`=".$this->eid)->getField("id,curtool,none_anim,front_pic,front_pic_url,front_cfg"));
		}
		if(!$cfg["frontcfg"]["front_cfg"]){
			// 如果没有设置外观则读取默认配置
			if(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]]){
				$cfg["frontcfg"]["front_cfg"]= json_decode(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]],true);
			}else{
				$cfg["frontcfg"]["front_cfg"]= json_decode(C("FRONT_DEFAULT")["default"],true);
			}
			// 直接读取默认的奖品绑定设置
			$giftset = json_decode(C("GIFTS_DEFAULT")[$cfg["frontcfg"]["curtool"]],true);
			$cfg["giftset"] = $giftset["giftset"];
			$cfg["onoff"] = $giftset["onoff"];
		}else{
			$this->issetted = 1;
			$cfg["frontcfg"]["front_cfg"]= json_decode(htmlspecialchars_decode($cfg["frontcfg"]["front_cfg"]),true);
			// 配置已保存的奖品绑定设置
			$cfg["giftset"]=$cfg["frontcfg"]["front_cfg"]["giftset"];
			$cfg["onoff"]=$cfg["frontcfg"]["front_cfg"]["onoff"];
			
			// 如果没有配置则读取默认奖品绑定设置
			if(!$cfg["giftset"]||!$cfg["onoff"]){
				$giftset = json_decode(C("GIFTS_DEFAULT")[$cfg["frontcfg"]["curtool"]],true);
				$cfg["giftset"] = $giftset["giftset"];
				$cfg["onoff"] = $giftset["onoff"];
			}
		}
		
		if(!$cfg["frontcfg"]["button_cfg"]){
			// 如果没有外观配置的按钮配置则读取默认
			$cfg["frontcfg"]["button_cfg"] = json_decode(C("BUTTON_DEFAULT"),true);
		}
		
		// 读取默认设置参数，用于前台读取默认参数
		if(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]]){
			$frount_default = (json_decode(C("FRONT_DEFAULT")[$cfg["frontcfg"]["curtool"]],true));
		}else{
			$frount_default = (json_decode(C("FRONT_DEFAULT")["default"],true));
		}
		$this->gift_default=json_decode(C("GIFTS_DEFAULT")[$cfg["frontcfg"]["curtool"]],true);
		$this->frount_default=$frount_default;
		return $cfg;
	}
	public function getMobCfg(){
		$cfg = current($this->where("`sid`=".$this->sid." AND `id`=".$this->eid)->getField("id,mobtool,curtool,notice,notice_rule,notice_receive,front_mob_cfg"));
		$cfg["front_mob_cfg"] = json_decode($cfg["front_mob_cfg"],true);
		return $cfg;
	}
}